package com.hntest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hntest.entity.LoginInfo;
import com.hntest.entity.Pager;
import com.hntest.entity.RealAuth;
import com.hntest.service.RealAuthService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("realAuthController")
public class RealAuthController {

    @Autowired
    private RealAuthService realAuthService;

    @RequestMapping(value = "/admin/select",method = {RequestMethod.GET,RequestMethod.POST})
    public String select(Pager pager, RealAuth realAuth, ModelMap modelMap){
        List<RealAuth> list = realAuthService.getListByPage(pager,realAuth);
        modelMap.put("list",list);
        modelMap.put("pager",pager);
        modelMap.put("realAuth",realAuth);
        return "admin/selectRealAuth";
    }


    @RequestMapping(value="/admin/doUpdate",method= {RequestMethod.GET,RequestMethod.POST},
            produces= {"application/JSON;charset=utf-8"})
    @ResponseBody
    public Object doUpdateRealAuth(RealAuth realAuth, HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        LoginInfo adminUser = (LoginInfo) session.getAttribute("adminUser");
        realAuth.setAuditor(adminUser);
        int n =  realAuthService.updateRealAuth(realAuth);
        RealAuth realAuth1 = null;
        JSONObject jsonObject = new JSONObject();
        if (n>0){
            realAuth1 = realAuthService.selectById(realAuth.getId());
            jsonObject.put("success",true);
            jsonObject.put("msg",realAuth1.getStateDisplay());
        }else{
            jsonObject.put("success",false);
            jsonObject.put("msg","系统异常，审核出错");
        }
        return JSON.toJSONString(jsonObject);
    }

    @RequestMapping(value = "/toInsert",method = {RequestMethod.GET,RequestMethod.POST})
    public String toInsertRealAuth( ModelMap modelMap,HttpServletRequest request){
        HttpSession session = request.getSession(true);
        LoginInfo currUser = (LoginInfo) session.getAttribute("currUser");
        RealAuth realAuth = realAuthService.selectByLoginId(currUser.getLid());
        System.out.println("xxxxxxxxx");
        System.out.println("------------------------------>>>>>"+realAuth);
        modelMap.put("realAuth",realAuth);
        return "insertRealAuth";
    }

    @RequestMapping(value = "/realauthdoInsert",method = {RequestMethod.GET,RequestMethod.POST},produces= {"application/JSON;charset=utf-8"})
    @ResponseBody
    public Object doInsertRealAuth( RealAuth realAuth,HttpServletRequest request){

        //System.out.println(realAuth);
        HttpSession session = request.getSession(true);
        LoginInfo currUser = (LoginInfo) session.getAttribute("currUser");
        realAuth.setApplier(currUser);
        int n = realAuthService.insertRealauth(realAuth);
        JSONObject jsonObject = new JSONObject();
        if (n>0){
            jsonObject.put("success",true);
        }
       // return "admin/insertRealAuth";
        return JSON.toJSONString(jsonObject);
    }

}
